<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form
              ref="form"
              :model="formData"
              :rules="formRules"
              label-width="120px"
              style="margin-left: 120px; margin-top:30px"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="formData.username" style="width:300px"/>
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input v-model="formData.password2" style="width:300px" type="password"/>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUserData">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <user-info @update="initData"/>
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <job-info/>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { editEmployeesApi, getEmployeesInfoApi } from '@/api/employees'
import JobInfo from '@/views/employees/components/jobInfo.vue'
import UserInfo from '@/views/employees/components/userInfo.vue'

export default {
  name: 'Detail',
  components: {
    JobInfo,
    UserInfo
  },
  data() {
    return {
      // 表单数据
      formData: {},
      // 表单规则
      formRules: {
        username: {
          required: true,
          message: '请输入姓名',
          trigger: 'blur'
        },
        password2: {
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }
      }
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 获取用户信息
    async initData() {
      const res = await getEmployeesInfoApi(this.$route.params.id)
      console.log(res)
      this.formData = res
    },
    // 保存用户信息
    async saveUserData() {
      try {
        await this.$refs.form.validate()
      } catch (e) {
        return console.log(e)
      }
      await editEmployeesApi({
        ...this.formData,
        password: this.formData.password2 // 新输入的密码覆盖原来的老密码
      })
      this.$message.success('更新成功')
    }
  }
}
</script>

<style scoped>

</style>
